package com.example.jasypt.http;

import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * <p>
 * [ok]成功响应, [fail]失败响应, [error]异常响应
 * </p>
 *
 * @author 王令
 * @since 2021-12-30
 */
@Data
@Accessors(chain = true)
@NoArgsConstructor
public class Response<T> implements Serializable {

    private static final long serialVersionUID = -5431344840208245994L;

    private Integer code;
    private String message;
    private T data;
    private Long timestamp;

    public Response(Integer code, String message) {
        this.code = code;
        this.message = message;
        this.timestamp = System.currentTimeMillis();
    }

    public Response(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.timestamp = System.currentTimeMillis();
    }

    public Response(ResultStatus status) {
        this.code = status.getCode();
        this.message = status.getMessage();
        this.timestamp = System.currentTimeMillis();
    }

    public Response(ResultStatus status, T data) {
        this.code = status.getCode();
        this.message = status.getMessage();
        this.data = data;
        this.timestamp = System.currentTimeMillis();
    }

    public static <T> Response<T> ok() {
        return ok(ResultEnum.HTTP_OK);
    }

    public static <T> Response<T> ok(Integer code) {
        return ok(code, ResultEnum.HTTP_OK_MSG);
    }

    public static <T> Response<T> ok(String message) {
        return ok(ResultEnum.HTTP_OK_CODE, message);
    }

    public static <T> Response<T> ok(Integer code, String message) {
        return new Response<>(code, message);
    }

    public static <T> Response<T> ok(T data) {
        return ok(ResultEnum.HTTP_OK, data);
    }

    public static <T> Response<T> ok(Integer code, String message, T data) {
        return new Response<>(code, message, data);
    }

    public static <T> Response<T> ok(ResultStatus status) {
        return new Response<>(status);
    }

    public static <T> Response<T> ok(ResultStatus status, T data) {
        return new Response<>(status, data);
    }

    public static <T> Response<T> setData(T data) {
        return ok(data);
    }

    public static <T> Response<T> fail() {
        return fail(ResultEnum.HTTP_FAIL);
    }

    public static <T> Response<T> fail(Integer code) {
        return fail(code, ResultEnum.HTTP_FAIL_MSG);
    }

    public static <T> Response<T> fail(String message) {
        return fail(ResultEnum.HTTP_FAIL_CODE, message);
    }

    public static <T> Response<T> fail(Integer code, String message) {
        return new Response<>(code, message);
    }

    public static <T> Response<T> fail(ResultStatus status) {
        return new Response<>(status);
    }

    public static <T> Response<T> error() {
        return error(ResultEnum.HTTP_ERROR);
    }

    public static <T> Response<T> error(String errMsg) {
        return error(ResultEnum.HTTP_ERROR_CODE, errMsg);
    }

    public static <T> Response<T> error(Integer code, String errMsg) {
        return new Response<>(code, errMsg);
    }

    public static <T> Response<T> error(ResultStatus status) {
        return new Response<>(status);
    }

    @Getter
    @RequiredArgsConstructor
    enum ResultEnum implements ResultStatus {
        /**
         * 请求成功
         */
        HTTP_OK(HTTP_OK_CODE, HTTP_OK_MSG),
        /**
         * 请求失败
         */
        HTTP_FAIL(HTTP_FAIL_CODE, HTTP_FAIL_MSG),
        /**
         * 服务器异常
         */
        HTTP_ERROR(HTTP_ERROR_CODE, HTTP_ERROR_MSG);

        private final Integer code;

        private final String message;
    }
}
